package com.example.tse.rbhttpservice.service.member;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberCouponGetList;
import com.example.tse.rbhttpservice.service.member.model.MemberCouponGetListRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberCouponGetListResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.log.L;

import java.lang.reflect.Type;
import java.util.List;


/**
 * @author wliu
 *         edi  tse
 */
public class MemberCouponServiceImpl implements MemberCouponService {
    private Context context;
    private static Gson gson = new Gson();


    public MemberCouponServiceImpl(Context context) {
        this.context = context;
    }

    /**
     * 获取用户的优惠券列表，根据状态获取不同的列表
     *
     * @param request
     * @param listener
     * @return MemberCouponGetListResponse
     */

    public void getList(MemberCouponGetListRequest request, final RemoteServiceListenerT<MemberCouponGetListResponse> listener) {

        final HttpParams params = new HttpParams();


        params.put("status", request.getStatus());

        final String url = "member/coupon/getList";


        /**
         coupon
         getList
         GetList
         获取用户的优惠券列表，根据状态获取不同的列表
         GET
         MemberCoupon
         MemberCouponGetList
         member/coupon/getList
         array
         */

        final Type type = new TypeToken<List<MemberCouponGetList>>() {
        }.getType();


        new HttpService().getList(context, url, params, MemberCouponGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            @Override
            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                List<MemberCouponGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                MemberCouponGetListResponse responce = new MemberCouponGetListResponse();
                responce.setGetList(items);
                listener.ok(responce);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                L.d(url + ":MemberCouponServiceImpl okData  getList  rudderResponse:" + rudderResponse);
                L.d(url + ":MemberCouponServiceImpl okData rudderResponse.getBody():" + rudderResponse.getBody());
//                if (rudderResponse.getBody() == null) {
                if (rudderResponse == null) {
                    listener.okData(new MemberCouponGetListResponse());
                } else {
                    List<MemberCouponGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                    MemberCouponGetListResponse responce = new MemberCouponGetListResponse();
                    responce.setGetList(items);
                    listener.okData(rudderResponse.getBody() == null ? new MemberCouponGetListResponse() : responce);
                }
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);


                if (rudderResponse == null) {
                    listener.okData(new MemberCouponGetListResponse(), fromWhere);
                } else {
                    List<MemberCouponGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                    MemberCouponGetListResponse responce = new MemberCouponGetListResponse();
                    responce.setGetList(items);
                    listener.okData(rudderResponse.getBody() == null ? new MemberCouponGetListResponse() : responce, fromWhere);
                }


            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }


        });
    }


}